package com.haiwen.boot.service.lmpl;

import com.haiwen.boot.mapper.UserMapper;
import com.haiwen.boot.pojo.EmailModel;
import com.haiwen.boot.pojo.User;
import com.haiwen.boot.service.LoginAndRegisterService;
import com.haiwen.boot.utils.JWTUtil;

import com.haiwen.boot.utils.MailSendUtils;
import com.haiwen.boot.utils.RedisUtil;
import com.haiwen.boot.utils.SaltUtils;
import com.haiwen.boot.vo.ResultMap;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import java.util.HashMap;
import java.util.Map;

@Service
public class LoginAndRegisterServiceImpl implements LoginAndRegisterService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    RedisUtil redisUtil;
    @Autowired
    ResultMap resultMap;
    @Autowired
    MailSendUtils mailSendUtils;
    @Override
    public ResultMap Register(String username,String password,String VerificationCode) {
        User user = userMapper.getPassword(username);
        if (user!=null){
            return resultMap.fail().code(401).message("此用户已存在，请登录");
        }
        String VerificationCodeCache =redisUtil.get("VerificationCode",String.class);
        if (VerificationCodeCache==null){
          return   resultMap.fail().code(401).message("验证码已失效，请重新获取!");
        }else if (!VerificationCodeCache.equals(VerificationCode)){
            return resultMap.fail().code(401).message("验证码错误");
        }
        //生成盐
        String salt = SaltUtils.getSalt(10);
        User user1 = new User();
        user1.setPassword(new Md5Hash(password,salt,2048).toHex());
        user1.setSalt(salt);
        user1.setUsername(username);
        redisUtil.remove("VerificationCode");
        int i = userMapper.insertSelective(user1);
        String token = JWTUtil.createToken(username);
        if (i>=0){
            redisUtil.set(token,token);
            return  resultMap.success().code(200).message("注册成功").token(token);
        }
        return  resultMap.fail().code(401).message("注册失败");
    }

    @Override
    public Map<String,Object> Login(String username, String password) {
        Map<String,Object> LoginHashMap=new HashMap<>();
        User user = userMapper.getPassword(username);
        if (user==null){
            LoginHashMap.put("code",401);
            LoginHashMap.put("msg","没有此用户");
        }else {
            String DBPassword = user.getPassword();
            String salt = user.getSalt();
            //解密
            Md5Hash Md5Hash=new Md5Hash(password,salt,2048);
            String md5Password = Md5Hash.toHex();
            if (!DBPassword.equals(md5Password)){
                LoginHashMap.put("code",401);
                LoginHashMap.put("msg","密码错误");
            }else {
                String token = JWTUtil.createToken(username +"."+user.getUserId());
                redisUtil.set(token,token);
                LoginHashMap.put("code",200);
                LoginHashMap.put("msg", "登录成功");
                LoginHashMap.put("token",token);
            }

        }
        return LoginHashMap;
    }

    @Override
    public ResultMap VerificationCode(String recieverName,String recieverEmailAddress) {
        String VerificationCode=(int)((Math.random()*9+1)*100000)+"";
        EmailModel emailModel=new EmailModel();
        emailModel.setRecieverName(recieverName);
        emailModel.setRecieverEmailAddress(recieverEmailAddress);
        emailModel.setEmailContent(VerificationCode);
        emailModel.setEmailTheme("Blog验证");
        try {
            mailSendUtils.sendEmailAsSysExceptionHtml(emailModel);
            redisUtil.set("VerificationCode",VerificationCode,110);
           return resultMap.success().code(200).message("发送成功");
        } catch (MessagingException e) {
            e.printStackTrace();
           return resultMap.fail().code(401).message("发送失败");
        }
    }
    //Sign out
    @Override
    public ResultMap SignOut(String token) {
        if (!redisUtil.hasKey(token)) return resultMap.success().code(500).message("未知异常！");
        redisUtil.remove(token);
        return resultMap.success().code(200).message("登出成功！");
    }


}
